package com.aacar.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aacar.constant.ConsFiled;
import com.aacar.dao.PersonalCarDao;
import com.aacar.dto.MyCarDto;
import com.aacar.entity.MyCar;
import com.aacar.entity.SeriesVo;
import com.aacar.service.BrandService;
import com.aacar.service.PersonalCarService;
import com.aacar.vo.BrandVo;
import com.aacar.vo.CommonVo;
import com.aacar.vo.MyCarVo;
//个人车辆管理
@Service("personalCarService")
public class PersonalCarServiceImpl implements PersonalCarService{
	
	@Autowired
	private PersonalCarDao personalCarDao;
	
	@Autowired
	private BrandService brandService;

	@Override
	public List<MyCarVo> getPersonalCar(String userId) {
		return this.personalCarDao.getPersonalCar(userId);
	}

	@Override
	public CommonVo managePersonalCar(MyCarDto myCarDto) {
		return null;
	}

	public CommonVo addPersonalCarRecord(MyCarDto myCarDto) {
		MyCar myCar = new MyCar();
		myCar.setUserId(myCarDto.getUserId());
		myCar.setPhoneNo(myCarDto.getPhoneNo());
		myCar.setCarNo(myCarDto.getCarNo());
		myCar.setBrandId(myCarDto.getBrandId());//品牌id
		myCar.setCarLibType(myCarDto.getCarLibType());//车类别,轿车 SUV
		myCar.setCarType(myCarDto.getCarType());//A类 B类 C类
		myCar.setSeriesName(myCarDto.getSeriesName());//车型名字
		myCar.setSeriesType(myCarDto.getSeriesType());//车型编号
		//根据brandid 获取品牌url 和 brandname
		BrandVo brand = this.brandService.getAllBrandByBrandId(myCarDto.getBrandId());
		if(null != brand) {
			myCar.setBrandName(brand.getBrandName());//品牌名字
			myCar.setBrandUrl(brand.getBrandUrl());//品牌url
		}
		//getPersonalCar
		SeriesVo vo = new SeriesVo();
		vo.setUserId(myCarDto.getUserId());
		vo.setSeriesName(myCarDto.getSeriesName());
		MyCarVo myVo = this.personalCarDao.getPersonalCarByUserIdAndName(vo);
		CommonVo cv = new CommonVo();
		if(null != myVo) {
			cv.setSuccess(false);
			cv.setInfo("该车系已经存在!");
			return cv;
		}
		int result = this.personalCarDao.addPersonalCarRecord(myCar);
		
		if(result > 0) {
			cv.setSuccess(true);
			cv.setInfo(ConsFiled.YES);
		}else {
			cv.setSuccess(false);
			cv.setInfo(ConsFiled.No);
		}
		return cv;
	}
	
	
	public CommonVo updatePersonalCarRecord(MyCarDto myCarDto) {
		MyCar myCar = new MyCar();
		myCar.setPid(myCarDto.getPid());
		myCar.setUserId(myCarDto.getUserId());
		myCar.setPhoneNo(myCarDto.getPhoneNo());
		myCar.setCarNo(myCarDto.getCarNo());
		if(myCarDto.getOperatorFlag().equals("3")) {
			myCar.setIsValid("0");
		}
		int result = this.personalCarDao.updatePersonalCarRecord(myCar);
		CommonVo cv = new CommonVo();
		if(result > 0) {
			cv.setSuccess(true);
			cv.setInfo(ConsFiled.YES);
		}else {
			cv.setSuccess(false);
			cv.setInfo(ConsFiled.No);
		}
		return cv;
	}

}
